package com.example.ecommercemarketing.handler.impl;

import com.example.ecommercemarketing.dto.MarketingElementsDTO;
import com.example.ecommercemarketing.dto.ProductDTO;
import com.example.ecommercemarketing.dto.UserDTO;
import com.example.ecommercemarketing.handler.MarketingElementsHandler;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 通过运营配置获取营销素材
 */
public class OperationalConfigHandler implements MarketingElementsHandler {

    @Override
    public MarketingElementsDTO handle(ProductDTO product, UserDTO user) {
        if (!product.isHasOperationalConfig()) {
            return null;
        }

        //这里直接写死了一些测试数据，实际项目中是通过发其它应用的接口、查数据库、经过一些繁琐的业务逻辑处理来获取数据的
        MarketingElementsDTO elements = new MarketingElementsDTO();
        elements.setRecommendText("【运营推荐】" + product.getName() + "，限时特惠！");
        elements.setProductHighlights("精选优质商品，平台大力推荐");
        elements.setMarketingImageUrl("https://example.com/marketing/operational/" + product.getId() + ".jpg");
        elements.setExtraInfo("运营专属活动");
        elements.setSource("operational_config");

        Random random = new Random();
        int randomTime = random.nextInt(15);
        //模拟实际业务中的耗时
        try {
            TimeUnit.MILLISECONDS.sleep(randomTime);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return elements;
    }
}